import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:intl/intl.dart';
import 'package:signage/l10n/l10n.dart';
import 'package:signage/widgets/custom_appbar.dart';
import '../controllers/cast_record_controller.dart';
import '../../../data/models/cast_record_model.dart';

class CastRecordView extends GetView<CastRecordController> {
  const CastRecordView({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color.fromRGBO(243, 244, 246, 1),
      appBar: CustomAppBar(
        backgroundColor: Colors.white,
        title: AppLocalizations.of(Get.context!)!.translate('screen_record'),
        rightButtons: [
          GestureDetector(
            onTap: () => controller.clearRecords(),
            child: Image.asset(
              'assets/images/Group 1321318725@2x.png',
              width: 20.w,
              height: 20.w,
            ),
          ),
          17.horizontalSpace,
        ],
      ),
      body: Obx(() {
        return Column(
          children: [
            8.verticalSpace,
            Container(
              height: 40.h,
              child: TabBar(
                controller: controller.tabController,
                labelPadding: EdgeInsets.symmetric(
                    horizontal: 10, vertical: 0), // 调整tab之间的间距
                isScrollable: false,
                indicator: null,
                indicatorColor: Colors.transparent,
                dividerHeight: 0,
                tabs: [
                  _buildTab(AppLocalizations.of(Get.context!)!.translate('all'), 0),
                  _buildTab(AppLocalizations.of(Get.context!)!.translate('success'), 1),
                  _buildTab(AppLocalizations.of(Get.context!)!.translate('fail'), 2),
                ],
              ),
            ),
            Expanded(
                child:
                    TabBarView(controller: controller.tabController, children: [
              _buildList(controller.records),
              _buildList(controller.successRecords),
              _buildList(controller.failRecords),
            ]))
          ],
        );
      }),
    );
  }

  Widget _buildList(List<CastRecord> cache) {
    return cache.isEmpty
        ? Center(
            child: Column(
              children: [
                Image.asset(
                  'assets/images/Frame@2x (9).png',
                  width: 201.w,
                  height: 201.w,
                ),
                const SizedBox(height: 16),
                Text(
                  AppLocalizations.of(Get.context!)!.translate('no_cast_record'),
                  style: TextStyle(
                    fontSize: 15,
                    fontWeight: FontWeight.normal,
                    letterSpacing: 0,
                    color: Color.fromRGBO(102, 102, 102, 1),
                  ),
                ),
              ],
            ),
          )
        : ListView.separated(
            padding: const EdgeInsets.all(16),
            itemCount: cache.length,
            separatorBuilder: (context, index) => const SizedBox(height: 12),
            itemBuilder: (context, index) => _buildRecordCard(cache[index]),
          );
  }

  Widget _buildTab(String label, int index) {
    return Container(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Text(
            label,
            style: TextStyle(
              fontSize: 16.sp,
              height: 26.h / 16.sp,
              color: Color(0XFF333333),
            ),
          ),
          controller.currentTab.value == index
              ? Image.asset(
                  'assets/images/Vector@2x.png',
                  width: 13.w,
                  height: 5.h,
                )
              : SizedBox(height: 5.h)
        ],
      ),
    );
  }

  Widget _buildRecordCard(CastRecord record) {
    return Card(
      color: Colors.white,
      elevation: 2,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          16.verticalSpace,
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              16.horizontalSpace,
              Expanded(
                child: Row(
                  children: [
                    Image.asset(
                      'assets/images/Group 1321318736@2x.png',
                      width: 34.w,
                      height: 24.w,
                    ),
                    16.horizontalSpace,
                    Expanded(
                      child: Text(
                        record.meetingTitle,
                        style: const TextStyle(
                          fontSize: 16,
                          fontWeight: FontWeight.w500,
                        ),
                      ),
                    ),
                  ],
                ),
              ),
              _buildStatusChip(record.success),
            ],
          ),
          Text(
            '${DateFormat('yyyy.MM.dd HH:mm').format(record.timestamp)}',
            style: const TextStyle(
              color: Color.fromRGBO(102, 102, 102, 1),
              fontSize: 14,
              fontWeight: FontWeight.w400,
              letterSpacing: 0,
            ),
          ).marginOnly(left: 66.w),
          const SizedBox(height: 12),
          Stack(
            children: [
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: [
                  Spacer(),
                  GestureDetector(
                      onTap: () => controller.recastRecord(record),
                      child: Container(
                        width: 100,
                        height: 32,
                        alignment: Alignment.center,
                        decoration: BoxDecoration(
                          color: Color.fromRGBO(252, 207, 1, 1),
                          borderRadius: BorderRadius.circular(28),
                        ),
                        child: Text(
                          AppLocalizations.of(Get.context!)!.translate('cast'),
                          style: TextStyle(
                            fontSize: 15,
                            fontWeight: FontWeight.w400,
                            color: Color.fromRGBO(51, 51, 51, 1),
                          ),
                        ),
                      )),
                  16.horizontalSpace,
                  GestureDetector(
                    onTap: () => controller.viewRecordDetail(record),
                    child: Container(
                      width: 100,
                      height: 32,
                      alignment: Alignment.center,
                      decoration: BoxDecoration(
                        color: Color.fromRGBO(239, 239, 239, 1),
                        borderRadius: BorderRadius.circular(28),
                      ),
                      child: Text(
                        AppLocalizations.of(Get.context!)!.translate('details'),
                        style: TextStyle(
                          fontSize: 15,
                          fontWeight: FontWeight.w400,
                          color: Color.fromRGBO(51, 51, 51, 1),
                        ),
                      ),
                    ),
                  ),
                  Spacer(),
                ],
              ),
              Positioned(
                right: 18.w,
                child: GestureDetector(
                  onTap: () => controller.deleteRecord(record),
                  child: Image.asset(
                    'assets/images/Group 1321318595@2x.png',
                    width: 24.w,
                    height: 24.w,
                  ),
                ),
              )
            ],
          ),
          16.verticalSpace,
        ],
      ),
    );
  }

  Widget _buildStatusChip(bool success) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 12.w, vertical: 4),
      decoration: BoxDecoration(
        color: success
            ? Color.fromRGBO(213, 252, 224, 1)
            : Colors.red.withOpacity(0.1),
        borderRadius: BorderRadius.only(
          topLeft: Radius.circular(78),
          bottomLeft: Radius.circular(78),
        ),
      ),
      child: Text(
        success ? AppLocalizations.of(Get.context!)!.translate('success') : AppLocalizations.of(Get.context!)!.translate('fail'),
        style: TextStyle(
          color: success ? Colors.green : Colors.red,
          fontSize: 12,
        ),
      ),
    );
  }

  Widget _buildActionButton(
      String label, IconData icon, Color color, VoidCallback onTap) {
    return InkWell(
      onTap: onTap,
      borderRadius: BorderRadius.circular(8),
      child: Container(
        padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 5),
        decoration: BoxDecoration(
          color: Color.fromRGBO(252, 207, 1, 1),
          borderRadius: BorderRadius.circular(28),
        ),
        child: Row(
          mainAxisSize: MainAxisSize.min,
          children: [
            Text(
              label,
              style: TextStyle(
                color: color,
                fontSize: 15.sp,
              ),
            ),
          ],
        ),
      ),
    );
  }
}
